package com.aidong.dynamic.action;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.dynamic.biz.TrnDynamicCommentBiz;
import com.aidong.dynamic.entity.TrnDynamicContent;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 运动圈评论发表action
 * 借助于运动圈的实体类 
 * @author java
 * @version 1.0  
 *  2015.6.4
 */
@ParentPackage("json")
@Namespace("/")
public class TrnDynamicCommentAction implements ModelDriven<TrnDynamicContent> {
	
	private TrnDynamicContent dynamicContent; // 用来接收与评论有关的数据

	@Override
	public TrnDynamicContent getModel() {
		if(dynamicContent == null){
			dynamicContent = new TrnDynamicContent();
		}
		return dynamicContent;
	}
	
	private TrnDynamicCommentBiz dynamicCommentBiz; // 运动圈评论service
	
	/**
	 * 运动圈评论service
	 * @param dynamicCommentBiz
	 */
	@Autowired
	public void setDynamicCommentBiz(TrnDynamicCommentBiz dynamicCommentBiz) {
		this.dynamicCommentBiz = dynamicCommentBiz;
	}
	
	private Map<String, Object> map; // 返回的json数据

	/**
	 * 返回的json数据
	 * @return
	 */
	@JSON
	public Map<String, Object> getMap() {
		return map;
	}

	/**
	 * 发表运动圈评论
	 * @return
	 */
	@Action(value="adddynamicomment",results={@Result(name="succ",type="json",params={"root","map"})})
	public String addDynamicComment(){
		map = new HashMap<String, Object>();
		try {
			int currentIdongId = dynamicContent.getCurrentIdongId();
			int currentRecipientId = dynamicContent.getCurrentRecipientId();
			int dynamicId = dynamicContent.getDynamicId();
			String content = dynamicContent.getContent();
			int length = content.length();	
			if (currentIdongId > 0 && dynamicId > 0
				   && (content != null && !"".equals(content))) {
				if (length > 1800) {
					map.put("code", 0);
					map.put("msg", "您到评论已超过字数限制!");
					return "succ";
				}
				Map<String, Object> paraMap = new HashMap<String, Object>();
				paraMap.put("currentIdongId", currentIdongId);
				paraMap.put("currentRecipientId", currentRecipientId);
				paraMap.put("dynamicId", dynamicId);
				paraMap.put("content", content);
				dynamicCommentBiz.addDynamicComment(paraMap);
				
				map.put("code", 1);
				map.put("result", "发表成功");
				return "succ";
			} else {
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
	
	/**
	 * 运动圈删除评论或者回复 
	 * @return
	 */
	@Action(value="deldynamicomment",results={@Result(name="succ",type="json",params={"root","map"})})
	public String delDynamicComment() {
		
		map = new HashMap<String, Object>();
		if (dynamicContent.getCommentId() > 0) {
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("commentID", dynamicContent.getCommentId());
			try {
				dynamicCommentBiz.delDynamicComment(paraMap);
				// 删除成功
				map.put("code", 1);
				map.put("result", "删除成功");
				return "succ";
			}catch(Exception e) {
				map.put("code", 0);
				map.put("msg", "系统异常");
				e.printStackTrace();
				return "succ";
			}
			
		}else{
			map.put("code", 0);
			map.put("msg", "参数缺失"); // 删除失败
			return "succ";
		}
	}
}
